﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace VTV.TaskManagementSystem.DataAccess
{
    public partial class Task : IConvertToLinqEntity<Task>, ISetting<Entities.Task>
    {
        public string ErrorMessage { get; set; }

        public Task Convert(object baseEntity)
        {
            Task result = null;
            try
            {
                if (baseEntity != null)
                {
                    var entity = (Entities.Task)baseEntity;

                    return result = new Task
                    {
                        TaskName = String.IsNullOrEmpty(entity.TaskName) ? null : entity.TaskName,
                        TaskID = entity.TaskID,
                        Descritions = String.IsNullOrEmpty(entity.Descriptions) ? null : entity.Descriptions
                    };
                }
                else
                {
                    ErrorMessage = Common.Constant.ErrorMessage.SOURCE_IS_NULL;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
            }

            return result;
        }

        public bool AddNew(Entities.Task source)
        {
            try
            {
                var newSource = this.Convert(source);

                using (var data = new DataBaseDataContext())
                {
                    data.Tasks.InsertOnSubmit(newSource);
                    data.SubmitChanges();
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }

            return true;
        }

        public bool Delete(Entities.Task source)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var newSource = data.Tasks.FirstOrDefault(p => p.TaskID == source.TaskID);

                    if (newSource != null)
                    {
                        //data.Tasks.Attach(newSource);
                        data.Tasks.DeleteOnSubmit(newSource);
                        data.SubmitChanges();

                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public bool Update(Entities.Task source)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var Task = data.Tasks.FirstOrDefault(p => p.TaskID == source.TaskID);
                    if (Task != null)
                    {
                        Task.TaskName = String.IsNullOrEmpty(source.TaskName) ? null : source.TaskName;
                        Task.Descritions = String.IsNullOrEmpty(source.Descriptions) ? null : source.Descriptions;

                        data.SubmitChanges();

                        return true;
                    }
                    else
                    {
                        ErrorMessage = Common.Constant.SqlErrorMessage.NOT_EXIST_OBJECT_IN_DB;

                        return false;
                    }
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public int? GetCount()
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    return data.Tasks.Count();
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        public IEnumerable<Entities.Task> GetAll(int start, int limit)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var obj = data.Tasks.OrderBy(p => p.TaskName).Skip(start).Take(limit).ToList();
                    var result = (from p in obj
                                  select new Entities.Task()
                                  {
                                      TaskID = p.TaskID,
                                      TaskName = String.IsNullOrEmpty(p.TaskName) ? string.Empty : p.TaskName,
                                      Descriptions = String.IsNullOrEmpty(p.Descritions) ? string.Empty : p.Descritions
                                  }).ToList();

                    return result;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        public Entities.Task GetTask(int taskID)
        {
            try
            {
                using (var data = new DataBaseDataContext())
                {
                    var result = (from p in data.Tasks
                                  where p.TaskID == taskID
                                  select new Entities.Task
                                  {
                                      TaskID = p.TaskID,
                                      TaskName = p.TaskName,
                                      Descriptions = p.Descritions
                                  }).FirstOrDefault();

                    return result;
                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }
    }
}
